﻿from django import template
from datetime import datetime,timedelta
from google.appengine.api import users
from app.functions import rfc3339
import urllib

register = template.Library()

def timezone(value, offset):
	return value + timedelta(hours=offset)

register.filter(timezone)

def gen_utf8(value):
	return value.decode('utf-8')

register.filter(gen_utf8)


def gen_dd(ts):
	tn = datetime.now()
	return abs(tn-ts).days

register.filter(gen_dd)

def gen_dh(ts):
	tn = datetime.now()
	ds = abs(tn-ts).seconds
	return dh/3600

register.filter(gen_dh)

def gen_dm(ts):
	tn = datetime.now()
	ds = abs(tn-ts).seconds
	return ds%3600/60

register.filter(gen_dm)

def split_users_name(userlist):
	try:
		userlist = [elem.email() for elem in userlist]
		userlist = ",".join(userlist)
		return userlist
	except:
		pass

register.filter(split_users_name)


def jieblognickname(value):
	nick = value.nickname().split("@")
	return nick[0]

register.filter(jieblognickname)

def get_rfc_datetime(datetime_data):
	return rfc3339.rfc3339(datetime_data)
	
register.filter(get_rfc_datetime)

def gendes(contents,length=50):
	n = int(length)
	content = contents[:n]
	return content

register.filter(gendes)

def classcolor(player_class):
	if player_class == "9":
		color_str = "#9482C9"
	elif player_class == "5":
		color_str = "#FFFFFF"
	elif player_class == "1":
		color_str = "#C79C6E"
	elif player_class == "7":
		color_str = "#2459FF"
	elif player_class == "4":
		color_str = "#FFF569"
	elif player_class == "2":
		color_str = "#F58CBA"
	elif player_class == "8":
		color_str = "#69CCF0"
	elif player_class == "3":
		color_str = "#ABD473"
	elif player_class == "11":
		color_str = "#FF7D0A"
	elif player_class == "6":
		color_str = "#C41F3B"
	else:
		color_str = "#666666"
	return color_str

register.filter(classcolor)

def classname(player_class):
	if player_class == "9":
		class_name = 'ca_warlock'
	elif player_class == "5":
		class_name = 'ca_priest'
	elif player_class == "1":
		class_name = 'ca_warrior'
	elif player_class == "7":
		class_name = 'ca_shaman'
	elif player_class == "4":
		class_name = 'ca_rogue'
	elif player_class == "2":
		class_name = 'ca_paladin'
	elif player_class == "8":
		class_name = 'ca_mage'
	elif player_class == "3":
		class_name = 'ca_hunter'
	elif player_class == "11":
		class_name = 'ca_druid'
	elif player_class == "6":
		class_name = 'ca_deathknight'
	return class_name

register.filter(classcolor)